class HeroManager {
    private static instance = new HeroManager();
    private constructor() { }
    static getInstance(): HeroManager {
        if (!HeroManager.instance) {
            HeroManager.instance = new HeroManager()
        }
        return this.instance
    }

    damageAttack: number = -10;//基础攻击力
    life: number = 100;//生命值
    lifeMax: number = 100;//生命值
    runSpeed: number = 150;//速度
    jumpCd: number = 0.2;//跳跃CD时间--1
    damageCd: number = 0.5;//普攻CD时间--2
    speedUpWayCd: number = 3;//Way加速CD时间--3
    speedUpHeroCd: number = 5;//Hero加速CD时间--4
    damageSpeed: number = 400;//子弹速度
    jumpSpeed: number = 500;//跳跃速度

    lifeRegain: number = 0.5;//生命恢复
    CommonImmunity: number = 0;//护甲(普攻抗性)
    AttributeImmunity: number = 0;//魔抗(属性攻击抗性)
    speedUpWay: number = 300;//漂浮跑道加速度
    speedUpWayTime: number = 1;//漂浮跑道加速时间
    speedUpHero: number = 1000;//加速度
    speedUpHeroTime: number = 1;//加速度时间

    HitCounts: number = 0;//被命中次数
    killCounts: number = 0;//击杀数
    passHitCounts: number = 0;//最多被命中次数
    passkillCounts: number = 0;//目标击杀数
    playTime: number = 120;//本局规定时间

    isFreeze: boolean = false;//冰冻状态
    isDizziness: boolean = false;//眩晕状态
    isRunState: boolean = false;//是否移动
    heroDie: boolean = false;
    faceTo: number = 0;//移动方向
    flyWaySpeed: number = 0;//相对FlyWay移动速度

    //自动生命回复
    updateLife(time: number) {
        if (this.life < this.lifeMax) {
            this.life += this.lifeRegain * time;
            this.life = this.life > this.lifeMax ? this.lifeMax : this.life;
        }
    }
    //设置血量
    SetLife(lifeValue: number) {
        this.life += lifeValue;
        if (this.life < 0) {
            this.life = 0;
        }
        else if (this.life > this.lifeMax) {
            this.life = this.lifeMax;
        }
    }
    //设置最大血量
    SetMaxLife(lifeValue: number): number {
        let _value = 0;
        this.lifeMax += lifeValue;
        if (this.lifeMax < 100) {
            _value = 100 - this.lifeMax;
            this.lifeMax = 100;
        }
        return _value;
    }
    //设置Hero移速
    SetRunSpeed(speedValue: number): number {
        let _value = 0;
        this.runSpeed += speedValue;
        if (this.runSpeed < 0) {
            _value = 0 - this.runSpeed;
            this.runSpeed = 0;
        }
        else if (this.runSpeed > 1000) {
            _value = 1000 - this.runSpeed;
            this.runSpeed = 1000;
        }
        return _value;
    }
    //设置移动状态
    SetRunState(runState: boolean, faceTo: number) {
        this.faceTo = faceTo;
        this.isRunState = runState;
    }
    //设置子弹飞行速度
    SetDamageSpeed(speedValue: number): number {
        let _value = 0;
        this.damageSpeed += speedValue;
        if (this.damageSpeed < 300) {
            _value = 300 - this.damageSpeed;
            this.damageSpeed = 300;
        }
        else if (this.damageSpeed > 1200) {
            _value = 1200 - this.damageSpeed;
            this.damageSpeed = 1200;
        }
        return _value;
    }
    //设置攻击力
    SetAttack(value: number): number {
        let _value = 0;
        this.damageAttack += value;
        if (this.damageAttack > 0) {
            _value = 0 - this.damageAttack;
            this.damageAttack = 0;
        }
        return _value;
    }
    //设置跳跃高度
    SetJumpSpeed(jumpSpeed: number): number {
        let _value = 0;
        this.jumpSpeed += jumpSpeed;
        if (this.jumpSpeed < 400) {
            _value = 400 - this.jumpSpeed;
            this.jumpSpeed = 400;
        }
        else if (this.jumpSpeed > 800) {
            _value = 800 - this.jumpSpeed;
            this.jumpSpeed = 800;
        }
        return _value;
    }

    /**
     * 设置技能CD
     * @param type 跳跃--1,攻击--2,Way加速--3,Hero加速--4
     * @param CdValue 加成值 
     */
    SetBtCD(type: number, CdValue: number) {
        switch (type) {
            case 1:
                {
                    this.jumpCd = this.BtCdCheck(this.jumpCd + CdValue);
                    break;
                }
            case 2:
                {
                    this.damageCd = this.BtCdCheck(this.damageCd + CdValue);
                    break;
                }
            case 3:
                {
                    this.speedUpWayCd = this.BtCdCheck(this.speedUpWayCd + CdValue);
                    break;
                }
            case 4:
                {
                    this.speedUpHeroCd = this.BtCdCheck(this.speedUpHeroCd + CdValue);
                    break;
                }
        }
    }

    /**
     * 当前移动速度
     */
    GetRunSpeed(): number {
        if (this.isFreeze || this.isDizziness)
            return 0;
        return this.runSpeed * this.faceTo + this.flyWaySpeed;
    }
    ReSetLife() {
        this.life = this.lifeMax;
        this.heroDie = false;
    }
    ReSetGame() {
        this.HitCounts = 0;
        this.killCounts = 0;
        this.passHitCounts = 0;
        this.passkillCounts = 0;
        this.playTime = 0;
        this.isFreeze = false;
        this.isDizziness = false;
        this.isRunState = false;
        this.heroDie = false;
        this.ReSetLife();
    }
    private BtCdCheck(value: number): number {
        if (value < 0.3) {
            value = 0.3;
        }
        else if (value > 30) {
            value = 30;
        }
        return value;
    }
}
(<any>window).HeroManager = HeroManager;
